This notebook shows how to write and test Google BigQuery Javascript UDFs (user-defined functions) within a notebook.
Before using this tutorial, you should go through the UDFs in BigQuery tutorial, which discusses how to use UDFs in notebooks without external code.
You can read more about UDFs here.
We will first write and test a UDF in a %%javascript cell. We will write the UDF code, then define test data, and finally call the UDF with the test data after creating a mock version of the input. Note that in a %%javascript cell you can access the output area element with the name element
; our mock function will use that element to produce output that is displayed in the notebook.
There is no state shared between %%javascript cells, so we have to perform the task in a single cell.
In [1]:
%%javascript
/**
* Our UDF function, which splits a set of URL query parameters into an array
*/
function getParameters(path) {
var re = /[?&]([^=]*)=([^&]*)/g;
var result = [];
var match;
while ((match = re.exec(path)) != null) {
result.push(decodeURIComponent(match[2]));
}
return result;
}
// Now we want to test the UDF. We can try calling it using a sample line from our table.
// Note that the variable `element` is available to us to create output in the notebook,
// so our test emitter will use that variable to display the fields.
var test_path = '/log/page?project=14&instance=81&user=16&page=master&path=63&version=0.1.1&release=alpha'
var test_results = ['14', '81', '16', 'master', '63', '0.1.1', 'alpha']
var params = getParameters(test_path);
if (JSON.stringify(params) == JSON.stringify(test_results)) {
element.append('OK')
} else {
element.append('Failed')
}
Looks like we are good to go!
If you have code that you use regularly in your UDFs, you can factor that out, put it in Google Cloud Storage, then import it; this technique is covered in the tutorial UDFs using Code in Cloud Storage.